//
//  YWSelectListOfTempmissionTableVC.m
//  maintain
//
//  Created by Kyo on 16/3/28.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWSelectListOfTempmissionTableVC.h"
#import "YWEquipmentManager.h"
#import "YWEquipment.h"

@interface YWSelectListOfTempmissionTableVC ()
@property (nonatomic, strong)NSMutableArray *equipments;
@end

@implementation YWSelectListOfTempmissionTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
}
-(NSMutableArray *)equipments{
    if (!_equipments) {
        NSMutableArray *mutableEqu = [NSMutableArray array];
        [self.qrcodeEquipmentInfoArray enumerateObjectsUsingBlock:^(YWQrcodeEquipmentInfo*  _Nonnull qrcodeEquipmentInfo, NSUInteger idx, BOOL * _Nonnull stop) {
            NSArray* equs = [[YWEquipmentManager shareManager] equipmentWithQrcodeId:qrcodeEquipmentInfo.qrcodeId orLocationId:0 equipmentTypeId:qrcodeEquipmentInfo.equipmentTypeId];
            if (equs) {
                [mutableEqu addObject:equs];
            }
        }];
        _equipments = mutableEqu;
    }
    return _equipments;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return self.equipments.count;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.maintenanceType ==YWMaintenanceTypeTrouble) {
        return [self.equipments[section]count];
    }else{
        return 1;
    }
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    
    static NSString* CellIdentifier = @"YWSelectListOfTempmissionTableVCCellIdentifier";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if (self.maintenanceType ==YWMaintenanceTypeTrouble) {
        YWEquipment *equipment = self.equipments[indexPath.section][indexPath.row];
        cell.textLabel.text = equipment.equipmentName?:@"未填写";
    }else{
        cell.textLabel.text = [self tableViewHeadTxt:indexPath.section];
    }
    return cell;
}
-(NSString *)tableViewHeadTxt:(NSInteger)section{
    YWQrcodeEquipmentInfo *qrcodeEquInfo = self.qrcodeEquipmentInfoArray[section];
    NSString *equipmentType = [[YWEquipmentManager shareManager] equipmentTypeNameWithTypeId:qrcodeEquInfo.equipmentTypeId];
    NSString *count = [NSString stringWithFormat:@"%ld个",(long)qrcodeEquInfo.equipmentCount];
    NSString *headText = [NSString stringWithFormat:@"%@  %@",equipmentType,count];
    return headText;
}
#pragma mark Table view delegate

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    return 40;
}
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    YWEquipment *equipment = self.equipments[indexPath.section][indexPath.row];
    YWQrcodeEquipmentInfo *qrcodeEquInfo = self.qrcodeEquipmentInfoArray[indexPath.section];
    if ([_delegate respondsToSelector:@selector(selectQrcodeEquipment:equipment:)]) {
        [_delegate selectQrcodeEquipment:qrcodeEquInfo equipment:equipment];
    }
}

@end
